//
//  JGRootViewController.m
//  JGNumberKeyBoard
//
//  Created by 古俊杰 on 2017/6/3.
//  Copyright © 2017年 古俊杰. All rights reserved.
//

#import "JGRootViewController.h"
#import "YWSearchCom.h"
#import "UIView+Category.h"
#import "JGSecondViewController.h"
#import "AFNetworkReachabilityManager.h"

#define NetWorkBarHeight 38
#define Main_Screen_Width       [[UIScreen mainScreen] bounds].size.width
#define Main_Screen_Height      [[UIScreen mainScreen] bounds].size.height
#define RGB(r, g, b)      [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]

@interface JGRootViewController () <UITableViewDelegate,UITableViewDataSource>
{
    BOOL _loseNetWorkNow;
}
@property (nonatomic, strong)UITableView *mainTable;

@property (nonatomic, strong)UIView *myHeaderView;

@property (nonatomic, strong)YWSearchCom *searchCom;

@property (nonatomic, strong)UIButton *netWorkstatus;

@end

@implementation JGRootViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.title = @"搜索";
    
    //---通知搜索界面（若还停留在搜索界面的话，应该隐藏tabbar和改变状态栏的颜色）-----
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification_ChangeSearchVCStyle" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //实例化tableView
    [self initTable];
    
    //实例化搜索框
    [self initSearchBar];
    
    //我们在做跳转时，需要一个跳转的环境，如果呈现的视图控制器不提供的话，需要呈现这个控制器的父视图控制器-----给searchVC跳转用
     self.definesPresentationContext = YES;
    
    //网络监控
    [self startCheckNetwork];
}

#pragma mark --  实例化tableView
- (void) initTable
{
    //tableView
    CGRect frame = self.view.bounds;
    self.mainTable = [[UITableView alloc]initWithFrame:frame style:UITableViewStyleGrouped];
    self.mainTable.delegate = self;
    self.mainTable.dataSource = self;
//    self.mainTable.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.mainTable];
    
}

#pragma mark -- 实例化搜索框
- (void)initSearchBar
{
    //table的头部试图
    self.myHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 44+NetWorkBarHeight)];
    
    //搜索框
    _searchCom = [[YWSearchCom alloc] initWithCurrentController:self];
    
    //网络中断时的提示view
    UIButton *netWorkstatus = [self setupAlertStatusBar:YES normalTitle:@"当前网络不可用，请检查您的网络状态" normalImageName:@"叹号"];
    self.netWorkstatus = netWorkstatus;
    self.netWorkstatus.top = 44;
    
    [self.myHeaderView addSubview:_searchCom.searchViewController.searchBar];
    [self.myHeaderView addSubview:self.netWorkstatus];
    //    设置tableview的头部试图
    self.mainTable.tableHeaderView = self.myHeaderView;
    
    self.myHeaderView.height = 44;
}


- (UIButton *)setupAlertStatusBar:(BOOL)isNetWorkShow normalTitle:(NSString *)title normalImageName:(NSString *)imageName
{
    UIButton *alertBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 0)];
    alertBtn.height = NetWorkBarHeight;
    [alertBtn setBackgroundColor:RGB(251, 182, 184)];
    [alertBtn setTitleColor:RGB(72, 72, 72) forState:UIControlStateNormal];
    alertBtn.hidden = YES;
    [alertBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [alertBtn setImageEdgeInsets:UIEdgeInsetsMake(3, 0, 3, 0)];
    [alertBtn setTitle:title forState:UIControlStateNormal];
    alertBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    alertBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    
    return alertBtn;
}

#pragma mark -- 监测网络情况
- (void)startCheckNetwork
{
    // 监测网络情况
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:// 未知
            {
                NSLog(@"网络状态未知");
                _loseNetWorkNow = NO;
                [self showStatusBar];
            }
            case AFNetworkReachabilityStatusNotReachable:// 无连接
            {
                NSLog(@"网络不可用");
                _loseNetWorkNow = YES;
                [self showStatusBar];
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN:// 3G 花钱
            {
                NSLog(@"数据流量");
                _loseNetWorkNow = NO;
                [self showStatusBar];
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi:// 局域网络,不花钱
            {
                NSLog(@"WiFi");
                _loseNetWorkNow = NO;
                [self showStatusBar];
                break;
            }
            default:
                break;
        }
    }];
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
}

-(void)showStatusBar
{
    self.mainTable.tableHeaderView = nil;
    if (_loseNetWorkNow)
    {
        self.netWorkstatus.hidden = NO;
        self.myHeaderView.height = 44 + NetWorkBarHeight;
        
    } else {
        self.netWorkstatus.hidden = YES;
        self.myHeaderView.height = 44;
    }
    self.mainTable.tableHeaderView = self.myHeaderView;
}


#pragma mark --------UITableViewDelegate,UITableViewDataSource---------

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}
- ( NSString *)tableView:( UITableView *)tableView titleForHeaderInSection:( NSInteger )section
{
    return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}


//section后面会有留白，所以要解决掉
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* identifier = @"SessionList";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                    reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
        //cell.accessoryType = UITableViewCellAccessoryNone;
        cell.backgroundColor = [UIColor whiteColor];
    }
    return cell;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.navigationController pushViewController:[JGSecondViewController new] animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
